local caishi = fk.CreateSkill {

  name = "joy__caishi",

  tags = {  },

}



caishi:addEffect(fk.EventPhaseEnd, {
  name = "joy__caishi",
  anim_type = "control",
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(caishi.name) and player == target and player.phase == Player.Draw then
      return #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        local move = e.data[1]
        if move and move.to and player.id == move.to and move.toArea == Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.DrawPile then
              return true
            end
          end
        end
      end, Player.HistoryPhase) > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local ids = {}
    player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      local move = e.data[1]
      if move and move.to and player.id == move.to and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.DrawPile then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
    end, Player.HistoryPhase)
    if #ids == 0 then return false end
    local different = table.find(ids, function(id) return Fk:getCardById(id).suit ~= Fk:getCardById(ids[1]).suit end)
    event:setCostData(self, different)
    if different and player:isWounded() then
      local card = player.room:askForDiscard(player, 1,1,true,caishi.name,true, ".", "#joy__caishi-invoke")
      if #card > 0 then
        player.room:throwCard(card,caishi.name,player)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local different = event:getCostData(self)
    if different then
      room:recover({ who = player,  num = 1, skillName = caishi.name })
    else
      room:setPlayerMark(player, "ty__caishi_twice-turn", 1)
    end
  end,
})

return caishi